/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jiadakeji.ppm.modules.business.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jiadakeji.ppm.common.config.Global;
import com.jiadakeji.ppm.common.persistence.Page;
import com.jiadakeji.ppm.common.web.BaseController;
import com.jiadakeji.ppm.common.utils.StringUtils;
import com.jiadakeji.ppm.modules.sys.utils.UserUtils;
import com.jiadakeji.ppm.modules.business.entity.CarTypePrice;
import com.jiadakeji.ppm.modules.business.service.CarTypePriceService;

/**
 * 车辆类型价格Controller
 * @author HHAOYU
 * @version 2019-07-09
 */
@Controller
@RequestMapping(value = "${adminPath}/business/carTypePrice")
public class CarTypePriceController extends BaseController {

	@Autowired
	private CarTypePriceService carTypePriceService;
	
	@ModelAttribute
	public CarTypePrice get(@RequestParam(required=false) String id) {
		CarTypePrice entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = carTypePriceService.get(id);
		}
		if (entity == null){
			entity = new CarTypePrice();
		}
		return entity;
	}
	
	@RequiresPermissions("business:carTypePrice:view")
	@RequestMapping(value = {"list", ""})
	public String list(CarTypePrice carTypePrice, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CarTypePrice> page = carTypePriceService.findPage(new Page<CarTypePrice>(request, response), carTypePrice); 
		model.addAttribute("page", page);
		
		UserUtils.putCache("carTypePrice", carTypePrice);
		return "modules/business/carTypePriceList";
	}

	@RequiresPermissions("business:carTypePrice:view")
	@RequestMapping(value = "form")
	public String form(CarTypePrice carTypePrice, Model model) {
		model.addAttribute("carTypePrice", carTypePrice);
		return "modules/business/carTypePriceForm";
	}

	@RequiresPermissions("business:carTypePrice:edit")
	@RequestMapping(value = "save")
	public String save(CarTypePrice carTypePrice, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, carTypePrice)){
			return form(carTypePrice, model);
		}
		carTypePriceService.save(carTypePrice);
		addMessage(redirectAttributes, "保存车辆类型价格成功");
		
		redirectAttributes.addFlashAttribute("carTypePrice", UserUtils.getCache("carTypePrice"));
		return "redirect:"+Global.getAdminPath()+"/business/carTypePrice/?repage";
	}
	
	@RequiresPermissions("business:carTypePrice:edit")
	@RequestMapping(value = "delete")
	public String delete(CarTypePrice carTypePrice, RedirectAttributes redirectAttributes) {
		carTypePriceService.delete(carTypePrice);
		addMessage(redirectAttributes, "删除车辆类型价格成功");
		
		redirectAttributes.addFlashAttribute("carTypePrice", UserUtils.getCache("carTypePrice"));
		return "redirect:"+Global.getAdminPath()+"/business/carTypePrice/?repage";
	}

}